Imports System.Web.Mail
Imports System.Collections.Specialized
Imports System.IO
Imports System.Text

Public MustInherit Class MailBase
    Public Sender As String = Config.AdminEmail
    Public Receiver As String
    Public Cc As String
    Public Subject As String
    Public MailFormat As Mail.MailFormat = MailFormat.Html
    Public Fields As NameValueCollection = New NameValueCollection
		
    Private body As String    

    Public MustOverride ReadOnly Property MailTemplatePath() As String

    Protected Overridable Sub PrepareFields()

    End Sub

    Private Function BuildMailBody() As String
        If (MailTemplatePath <> Nothing AndAlso MailTemplatePath.Length > 0) Then
            Dim mailBody As String
            Dim sReader As StreamReader = New StreamReader(MailTemplatePath)
            mailBody = sReader.ReadToEnd()
            sReader.Close()

            Dim sb As StringBuilder = New StringBuilder(mailBody)
            Dim strField As String = ""
            For Each key As String In Fields.Keys
                strField = Fields(key)
                strField = strField.Replace(Chr(13), "<br>")
                'sb.Replace(key, Fields(key))
                sb.Replace(key, strField)
            Next

            Return sb.ToString()
        Else
            Return ""
        End If
    End Function

    Public SenderPassword As String = Config.AdminEmailPassword

    Public Sub Send()
        PrepareFields()

        Dim message As MailMessage = New MailMessage
        message.Cc = Me.Cc
        message.To = Me.Receiver
        message.From = Config.AdminEmail
        message.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = Config.SmtpPort
        message.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = Config.SmtpUseSSL

        If (SenderPassword <> "") Then
            message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1") '	//basic authentication
            message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", Me.Sender) ' //set your username here			
            message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", Me.SenderPassword) '	//set your password here						
        End If

        message.Subject = Me.Subject
        message.BodyFormat = Me.MailFormat
        message.BodyEncoding = System.Text.Encoding.UTF8
        message.Body = Me.BuildMailBody()

        SmtpMail.SmtpServer = Config.SmtpServer
        SmtpMail.Send(message)
    End Sub

    Public Sub Send(ByVal fromEmail As String)
        PrepareFields()

        Dim message As MailMessage = New MailMessage
        message.Cc = Me.Cc
        message.To = Me.Receiver
        message.From = fromEmail
        message.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = Config.SmtpPort
        message.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = Config.SmtpUseSSL

        If (SenderPassword <> "") Then
            message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1") '	//basic authentication
            message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", Me.Sender) ' //set your username here			
            message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", Me.SenderPassword) '	//set your password here						
        End If

        message.Subject = Me.Subject
        message.BodyFormat = Me.MailFormat
        message.BodyEncoding = System.Text.Encoding.UTF8
        message.Body = Me.BuildMailBody()

        SmtpMail.SmtpServer = Config.SmtpServer
        SmtpMail.Send(message)
    End Sub
End Class
